<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/7
 * Time: 9:42
 * 卡片管理模块
 */
namespace app\home\model;

use app\home\model\Base;
use think\Model;
use think\Request;
use think\Db;
use think\Exception;
use think\Log;
use think\Session;

class Card extends Base {

    /**
     * 构造
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取的登录用户的卡片列表
     * 默认每页显示20条数据
     * @return array ['card'=>卡片列表数组,'param'=>获得的请求参数,'search'=>拼接的搜索条件]
     */
    public function getCardListM()
    {
        try {
            $param = Request::instance()->param();
            $where['a.cid'] = ['eq',$this->user_info['id']];
            $search = '';   // 拼接搜索条件，返回
            $iccid = isset($param['iccid']) ? trim($param['iccid']) : '';
            if($iccid){   // 卡片iccid
                $where['a.iccid'] = ['like',"%".$iccid."%"];
                $search .= '&iccid='.$iccid;
            }
            if(isset($param['status']) && $param['status'] != ''){   // 卡片状态
                $where['c.status'] = ['eq',$param['status']];
                $search .= '&status='.$param['status'];
            }
            $total = isset($param['total']) ? trim($param['total']) : '';
            if($total){   // 卡片规格
                $where['p.total'] = [['eq',$total.'M'],['eq',$total.'G'],'or'];
                $search .= '&total='.$total;
            }
            if(isset($param['globalsimtype']) && $param['globalsimtype'] != ''){   // 卡片运营商
                $where['c.globalsimtype'] = ['eq',$param['globalsimtype']];
                $search .= '&globalsimtype='.$param['globalsimtype'];
            }
            $list = Db::name('agent_card')->alias('a')
                ->field('a.`iccid`,c.`status`,c.`pkg_used`,c.`globalsimtype`,p.`total`,p.`pkg_kind`')
                ->join('card c','a.iccid = c.iccid')
                ->join('pkg p','a.pid = p.id')
                ->where($where)
                ->order('a.id desc')
                ->paginate(20,false);
            $card = $list->toArray();
            if(!$card['data']) {   // 不存在卡片数据直接返回
                return ['card'=>$card,'param'=>$param];
            }
            foreach($card['data'] as &$value){   // 处理卡片数据
                $value['status'] = $this->card_status[$value['status']];
                $value['globalsimtype'] = $this->globalsimtype[$value['globalsimtype']];
                $value['pkg_used'] = $this->unitConvert($value['pkg_used']);
                $value['pkg_kind'] = $this->package_type[$value['pkg_kind']];
            }
            return ['card'=>$card,'param'=>$param,'search'=>$search];
        } catch (Exception $e) {
            Log::write('获取卡片列表异常：'.$e->getMessage(),'error');
        }
    }

    /**
     * 卡片已使用量的换算
     * @param string|int $used 卡片已使用量（单位：字节）
     */
    protected function unitConvert($used = 0)
    {
        if(!$used){   // 未使用流量
            return '0M';
        }
        // 默认转换成M,保留三位小数
        $used_m = sprintf("%.3f",$used/(1024*1024));
        // 单位
        $unit = 'M';
        if($used_m >= 1024){
            $used_m = sprintf("%.3f",$used/(1024*1024*1024));
            $unit = 'G';
        }
        return $used_m.$unit;
    }

}